<template>
	<el-dialog title="核对状态" :visible.sync="addComShow" width="70%" :before-close="cancel">
		<div v-loading="loading" element-loading-text="正在提交，文件较多，请耐心等待" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
			<el-form style="display: flex">
				<el-form-item label="文件类型：" required style="display: flex; margin-right: 16px">
					<el-select filterable clearable v-model="fileCode">
						<el-option v-for="(item, index) in fileTypeCodesList" :key="index + 'g'" :label="item.typeName" :value="item.typeCode"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="生效时间：" style="display: flex">
					<el-date-picker type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="选择日期" v-model="date1"></el-date-picker>
				</el-form-item>
			</el-form>
			<div style="margin-bottom: 22px; padding-left: 83px; padding-top: 10px">
				<div style="color: red" v-if="fileCode === 'applyAgreementId'">（‘是否指数基金’为‘是’，则可上传文件）</div>
				<div style="color: red" v-if="fileCode === 'tradeNoActionLetterId'">（‘是否上市交易’为‘是’，则可上传文件）</div>
			</div>

			<span class="titlene" style="color: #389a39">匹配成功文件</span>
			<el-table :data="successList" border style="width: 100%">
				<el-table-column prop="fileName" label="文件名"> </el-table-column>
				<el-table-column prop="fileName" label="选择基金">
					<template slot-scope="scope">
						<el-select v-model="scope.row.fundSysCode" clearable filterable size="medium" style="width: 100%">
							<el-option key="" label="--全部--" value=""> </el-option>
							<el-option v-for="(item, index) in fundList" :key="index + item.fundSysCode" :label="item.fundCode + '-' + item.fundFullName" :value="item.fundSysCode"> </el-option>
						</el-select>
					</template>
				</el-table-column>
			</el-table>

			<span class="titlene" style="color: red">匹配失败文件</span>
			<el-table :data="errorList" border style="width: 100%">
				<el-table-column prop="fileName" label="文件名"> </el-table-column>
				<el-table-column prop="fileName" label="选择基金">
					<template slot-scope="scope">
						<el-select v-model="scope.row.fundSysCode" clearable filterable size="medium" style="width: 100%">
							<el-option key="" label="--全部--" value=""> </el-option>
							<el-option v-for="(item, index) in fundList" :key="index + item.fundSysCode" :label="item.fundCode + '-' + item.fundFullName" :value="item.fundSysCode"> </el-option>
						</el-select>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<span slot="footer" class="dialog-footer">
			<el-button size="small" @click="cancel">取 消</el-button>
			<el-button type="primary" size="small" @click="submit">提 交</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { getEnum } from '@/api/productApi/file'
import { getFundList } from '@/api/productApi/mechanism'
import { yaerSubmit } from '@/api/productApi/annReport'
import { timerConversionDate } from '@/filters'
export default {
	props: {
		addComShow: {
			type: Boolean,
			default: false
		},
		uploadType: {
			type: String,
			default: ''
		},
		comIsShow: {
			type: Function,
			default: Function
		},
		getUpdateData: {
			type: Function,
			default: Function
		}
	},
	data() {
		return {
			date1: new Date(),
			fileCode: '',
			fileTypeCodesList: [],
			successList: [],
			errorList: [],
			fundList: [],
			loading: false
		}
	},
	mounted() {
		this.getEunmData()
		this.getFundList()
	},
	methods: {
		submit() {
			if (!this.fileCode) {
				this.$message({ type: 'error', message: '请选择文件类型' })
				return false
			}
			const obj = {
				fileCode: this.fileCode,
				type: this.uploadType,
				effectiveDate: timerConversionDate(this.date1 ? this.date1 : new Date())
			}
			obj.productFileRelationDTOS = [...this.successList, ...this.errorList]
			if (obj.productFileRelationDTOS.some(item => !item.fundSysCode)) {
				this.$confirm('基金为空的文件，将不予保存到数据库，是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						obj.productFileRelationDTOS = obj.productFileRelationDTOS.filter(item => !!item.fundSysCode)
						this.loading = true
						yaerSubmit(obj).then(res => {
							if (res.code === 0) {
								if (res.data.body.successList && Array.isArray(res.data.body.successList) && res.data.body.successList.length) {
									this.$message({ type: 'success', message: res.msg })
								}
								this.$emit('getUpdateData', res.data.body)
								this.cancel()
							}
							this.loading = false
						})
					})
					.catch(() => {})
			} else {
				this.operatorComfirm(() => {
					this.loading = true
					yaerSubmit(obj).then(res => {
						if (res.code === 0) {
							if (res.data.body.successList && Array.isArray(res.data.body.successList) && res.data.body.successList.length) {
								this.$message({ type: 'success', message: res.msg })
							}
							this.$emit('getUpdateData', res.data.body)
							this.cancel()
						}
						this.loading = false
					})
				}, `保存匹配文件`)
			}
		},

		cancel() {
			this.$emit('comIsShow')
			this.errorList = []
			this.successList = []
			this.date1 = new Date()
			this.fileCode = ''
		},
		getEunmData() {
			getEnum(['fileTypeCodes']).then(res => {
				if (res.code === 0) {
					this.fileTypeCodesList = res.data.body.fileTypeCodes
				}
			})
		},
		edit(data) {
			this.successList = data.filter(item => !!item.fundSysCode)
			this.errorList = data.filter(item => !item.fundSysCode)
		},
		// 基金列表（下拉框）
		getFundList() {
			getFundList({}).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						if (item.baseInfo) {
							item.fundFullName = item.baseInfo.fundFullName
							item.fundName = item.baseInfo.fundName
						}
					})
					this.fundList = res.data.body
				} else {
					this.$message({ message: res.msgDesc, type: 'error' })
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped >
.titlene {
	height: 40px;
	display: block;
	font-size: 18px;
	line-height: 40px;
	font-weight: 600;
}
.el-form-item {
	margin-bottom: 0;
}
</style>

